package com.test.baseGroovy

import com.test.groovy.Song
import groovy.json.JsonOutput
import groovy.json.JsonSlurper

/**
 * JsonTest--GroovyTest
 *
 * @author: Jovi
 *
 * @createTime: 2018-06-13 10:16
 *
 * */
Song song = new Song()
song.name = "ge"
song.artist = "ni"

def json = JsonOutput.toJson(song) //JsonOutput 类把对象转换成 json字符串
println json

JsonSlurper jsonSlurper = new JsonSlurper()
Song song1 = jsonSlurper.parseText(json)  //JsonSlurper 类把 json 字符串转换成对象
println song1.name

/*=====================集合对象转json 、json 转集合对象=========================*/
def list = [song, song1]

def jsonArray = JsonOutput.toJson(list)

println jsonArray

List<Song> list2 = jsonSlurper.parseText(jsonArray)
println(list2.get(1).name)